package com.example.document.local

import com.example.lib_base.BaseRepository
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import java.io.File

class LocalRepository :BaseRepository(){

    fun scanFile():Flow<List<LocalEntity>>{
        return flow {
            val arrayList = ArrayList<LocalEntity>()
            val file = File("/storage/emulated/0/Android/data/com.example.mdm4/files/Download")
            //获取该目录下的所有列表
            val list = file.listFiles()
            for (it in list){
                //判断是不是文件类型
                if(it.isFile){
                    var localType:LocalType?=null
                    val s = it.path.substring(it.path.lastIndexOf("."))
                    if(s==".pdf"||s==".txt"||s==".doc"){
                        localType=LocalType.DOCUMENTS
                    }else if (s==".mp3"){
                        localType=LocalType.MUSIC
                    }else if(s==".mp4"){
                        localType=LocalType.VIDEO
                    }else if(s==".jpg"||s==".png"||s==".webp"){
                        localType=LocalType.IMG
                    }else{
                        continue
                    }
                    val entity = LocalEntity(it.name, it.path, localType!!,false)
                    arrayList.add(entity)
                }
            }
            emit(arrayList)
        }
    }
}